<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/Sanpham.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/DanhMuc.php';
$sanphamOb = new Sanpham();
$danhmucOb = new DanhMuc();
$danhmucOb->danhmucCombox();
$sanpham = array(
    'id' => null,
    'ten' => null,
    'danhmuc_id' => null,
    'gia' => null,
    'mota' => null,
    'dacbiet'=>0,
    'hinhanh'=>null,
    'trangthai'=>1
);
if (isset($_GET['id'])) {
    $sanpham = $sanphamOb->laySanPham($_GET['id']);
}

if (isset($sanpham['id'])) {
    $command = 'edit';
} else {
    $command = 'new';
}
$message = array();
if (isset($_POST['capnhat'])) {
    if ($_POST['command'] == 'new') {
        if($sanphamOb->themmoiSanPham($_POST)){     
            $sanphamOb->uploadFile($_FILES['hinhanh'],DataProvider::$LAST_INSERT_ID);
            header('Location: /admin/sanpham.php');
        }else{
            $message[]="Cập nhật thất bại";
        }
    } else {
        if($sanphamOb->capnhapSanPham($_GET['id'], $_POST)){
            if(isset($_POST['xoa_anh']) && $_POST['xoa_anh'] == 'yes'){
                $isDel = true;
            }else{
                $isDel = false;
            }
            $sanphamOb->uploadFile($_FILES['hinhanh'],$_GET['id'],'edit',$isDel);
            header('Location: /admin/sanpham.php');
        }else{
            $message[]="Cập nhật thất bại";
        }
    }
}

?>
<form method="post" action="" enctype="multipart/form-data">
    <input type="hidden" value="<?php echo $command ?>" name="command"/>
    <table style="margin: 10px auto" width="90%" border="1" align="center" cellspacing="1">
        <tr>
            <td bgcolor="#00CCFF" colspan="2"><p align="center"><b>Thông tin sản phẩm</b></p></td>         
        </tr>
        <?php if (count($message) > 0): ?>
            <tr>
                <td colspan="2">
                    <?php foreach ($message as $mes): ?>
                        <p><?php echo $mes ?></p>
                    <?php endforeach; ?>
                </td>         
            </tr>
        <?php endif; ?>
        <tr>
            <td width="150px" align="right">Tên sản phẩm:</td>
            <td><input type="text" value="<?php echo $sanpham['ten'] ?>"  name="ten" id="ten" size="40px" /></td>
        </tr>
        <tr>
            <td width="150px" align="right">Thuộc danh mục:</td>
            <td>
                <select name="danhmuc_id" id="danhmuc_id">
                    <option value="">[ Chọn ]</option>
                    <?php foreach ($danhmucOb->danhmucCombox() as $value=>$title): ?>
                            <option <?php echo ($value == $sanpham['danhmuc_id']) ? "selected" : ""; ?> value="<?php echo $value ?>"><?php echo $title ?></option>
                    <?php endforeach; ?>
                </select> 
            </td>
        </tr>
        <tr>
            <td width="150px" align="right">Giá:</td>
            <td>
               <input type="text" value="<?php echo $sanpham['gia'] ?>"  name="gia" id="gia" size="40px" />
            </td>
        </tr>
        <tr>
            <td width="150px" align="right">Mô tả:</td>
            <td>
                <textarea class="ckeditor"  name="mota" id="mota" style="width: 100%;height: 300px"><?php echo htmlspecialchars($sanpham['mota'],ENT_QUOTES,'UTF-8') ?></textarea>
            </td>
        </tr>
        <tr>
            <td width="150px" align="right">Đặc biệt:</td>
            <td>
                <select name="dacbiet" id="dacbiet">
                    <?php foreach(Sanpham::$DUNG_SAI_COMBO as $value=>$title):?>
                        <option <?php echo ($value == $sanpham['dacbiet']) ? "selected" : ""; ?>value="<?php echo $value ?>"><?php echo $title ?></option>
                    <?php endforeach; ?>    
                </select>
            </td>
        </tr>
        <tr>
            <td width="150px" align="right">Hình ảnh:</td>
            <td>
                <?php if ($command == 'edit' && !empty($sanpham['hinhanh']) && file_exists($_SERVER['DOCUMENT_ROOT'].$sanpham['hinhanh'])): ?>
                    <img width="150px" src="<?php echo $sanpham['hinhanh'] ?>" />
                    <input type="checkbox" value="yes" name="xoa_anh"/> Xóa ảnh
                <?php endif; ?>
                <input type="file" value="<?php echo $sanpham['hinhanh'] ?>"  name="hinhanh" id="hinhanh" size="40px" />
            </td>
        </tr>
        <tr>
            <td width="150px" align="right">Trạng thái:</td>
            <td>
                <select name="trangthai" id="trangthai">
                    <?php foreach(Sanpham::$AN_HIEN_COMBO as $value=>$title):?>
                        <option <?php echo ($value == $sanpham['trangthai']) ? "selected" : ""; ?> value="<?php echo $value ?>"><?php echo $title ?></option>
                    <?php endforeach; ?>  
                </select>
            </td>
        </tr>
        <tr>
            <td align="right"><input type="submit" name="capnhat" id="capnhat" value="Cập nhật"</td>
        </tr>
    </table>
</form>